package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.videoapp.portal.Exception.ServiceException;
import mr.ali.videoapp.portal.mapper.CollectionIndexSelectionMapper;
import mr.ali.videoapp.portal.model.CollectionIndexSelection;
import mr.ali.videoapp.portal.service.ICollectionIndexSelectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-06-17
 */
@Service
public class CollectionIndexSelectionServiceImpl extends ServiceImpl<CollectionIndexSelectionMapper, CollectionIndexSelection> implements ICollectionIndexSelectionService {

    @Resource
    private CollectionIndexSelectionMapper collectionIndexSelectionMapper;

    @Override
    public PageInfo<CollectionIndexSelection> findAll(Integer pageNum, Integer pageSize, Integer type) {

        // 判断翻页参数是否为空
        if (pageNum == null || pageSize == null) {
            // 抛出翻页参数错误异常
            throw ServiceException.GONE("翻页参数错误");
        }
        // 创建查询条件包装器
        QueryWrapper<CollectionIndexSelection> wrapper = new QueryWrapper<>();

        wrapper.eq("type", type);
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 根据查询条件获取视频列表
        List<CollectionIndexSelection> videoList = collectionIndexSelectionMapper.selectList(wrapper);

        return new PageInfo<>(videoList);
    }
}
